package com.mz.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.demo.entity.SysPermission;
import com.mz.demo.entity.SysRole;
import com.mz.demo.entity.SysUser;
import com.mz.demo.entity.SysUserRole;
import com.mz.demo.entity.vo.UserQuery;
import com.mz.demo.entity.vo.UserVo;
import com.mz.demo.service.UserService;
import com.mz.demo.utils.entity.PaginationVo;
import com.mz.demo.utils.entity.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 1、新增用户
     * */
    @PostMapping("")
    public R addUser( @RequestBody UserVo userVo ){

        userService.addUser(userVo);
        return R.ok();
    }

    /**
     * 2、获取用户分页列表
     * */
    @GetMapping("/pagination")
    public R getUserPagination(PaginationVo paginationVo, UserQuery userQuery) {

        IPage page = userService.getUserPagination( paginationVo, userQuery );
        return R.ok( paginationVo, page );
    }

    /**
     * 3、获取用户详情
     * */
    @GetMapping("")
    public R getUserDetailById( Long userId ){

        SysUser sysUser = userService.getUserDetailById( userId );
        return R.ok(sysUser);
    }


    /**
     * 4、修改用户
     * */
    @PutMapping("")
    public R updateUserInfo( @RequestBody UserVo userVo ){

        userService.updateUserInfo(userVo);
        return R.ok();
    }


    /**
     * 5、删除用户
     * */
    @DeleteMapping("")
    public R removeUserById( Long userId ){

        userService.removeUserById( userId );
        return R.ok();
    }


    @GetMapping("/myRole")
    public R getUserRoleList( Long userId ){

        List<SysRole> roleList = userService.getUserRoleList( userId );
        return R.ok(roleList);
    }

    @GetMapping("/myPermission")
    public R getUserPermissionList( Long userId ){

        List<SysPermission> permissionList =  userService.getUserPermissionList(userId);
        return R.ok(permissionList);
    }

}
